package com.house.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.Role;
import com.house.common.core.domain.entity.User;
import com.house.common.core.domain.model.LoginUser;
import com.house.common.core.page.TableDataInfo;
import com.house.common.exception.ServiceException;
import com.house.system.service.IHouseService;
import com.house.system.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.Appointment;
import com.house.system.service.IAppointmentService;

/**
 * 预约看房Controller
 *
 * @author tc
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/domain/appointment")
public class AppointmentController extends BaseController {
    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IHouseService houseService;

    /**
     * 查询预约看房列表
     */
    @PreAuthorize("@ss.hasPermi('domain:appointment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Appointment appointment) {
        startPage();
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        return getDataTable(list);
    }

    /**
     * 获取预约看房详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:appointment:query')")
    @GetMapping(value = "/{appointmentId}")
    public AjaxResult getInfo(@PathVariable("appointmentId") Long appointmentId) {
        return AjaxResult.success(appointmentService.selectAppointmentByAppointmentId(appointmentId));
    }

    /**
     * 获取预约看房用户和房屋详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:appointment:query')")
    @GetMapping(value = "/{userId}/{houseId}")
    public AjaxResult getOtherInfo(@PathVariable("userId") Long userId, @PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        User user = userService.selectUserByUserId(userId);
        House house = houseService.selectHouseByHouseId(houseId);
        ajax.put("user", user);
        ajax.put("house", house);
        return ajax;
    }

    /**
     * 新增预约看房
     */
    @PreAuthorize("@ss.hasPermi('domain:appointment:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Appointment appointment) {
        AjaxResult ajax = AjaxResult.success();
        //获取当前登录用户的id作为预约看房用户
        appointment.setUserId(getUserId());
        appointment.setIsHandle("0"); //未处理
        List<Appointment> appointments = appointmentService.selectAppointmentList(appointment);
        if (appointments.size() > 0) {
            //说明申请过,不需要重复申请
            throw new ServiceException("请勿重复提交,等待中介处理", 400);
        } else {
            appointmentService.insertAppointment(appointment);
        }

        return ajax;
    }

    /**
     * 修改预约看房
     */
    @PreAuthorize("@ss.hasPermi('domain:appointment:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Appointment appointment) {
        return toAjax(appointmentService.updateAppointment(appointment));
    }

    /**
     * 中介处理预约看房
     */
    @PreAuthorize("@ss.hasPermi('domain:appointment:handle')")
    @PutMapping("/handle")
    public AjaxResult handleAppointment(@RequestBody Appointment appointment) {
        AjaxResult ajax = AjaxResult.success();
        appointment.setIsHandle("1");
        appointmentService.updateAppointment(appointment);
        return ajax;
    }

    /**
     * 删除预约看房
     */
    @PreAuthorize("@ss.hasPermi('domain:appointment:remove')")
    @DeleteMapping("/{appointmentIds}")
    public AjaxResult remove(@PathVariable Long[] appointmentIds) {
        return toAjax(appointmentService.deleteAppointmentByAppointmentIds(appointmentIds));
    }
}
